package com.tledu.course.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.tledu.course.domain.FileManagement;
import com.tledu.course.service.IFileManagementService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文件管理Controller
 * 
 * @author Ghost
 * @date 2021-07-13
 */
@Controller
@RequestMapping("/course/file")
public class FileManagementController extends BaseController
{
    private String prefix = "course/file";

    @Autowired
    private IFileManagementService fileManagementService;

    @RequiresPermissions("course:file:view")
    @GetMapping()
    public String file()
    {
        return prefix + "/file";
    }

    /**
     * 查询文件管理列表
     */
    @RequiresPermissions("course:file:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FileManagement fileManagement)
    {
        startPage();
        List<FileManagement> list = fileManagementService.selectFileManagementList(fileManagement);
        return getDataTable(list);
    }

    /**
     * 导出文件管理列表
     */
    @RequiresPermissions("course:file:export")
    @Log(title = "文件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FileManagement fileManagement)
    {
        List<FileManagement> list = fileManagementService.selectFileManagementList(fileManagement);
        ExcelUtil<FileManagement> util = new ExcelUtil<FileManagement>(FileManagement.class);
        return util.exportExcel(list, "文件管理数据");
    }

    /**
     * 新增文件管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存文件管理
     */
    @RequiresPermissions("course:file:add")
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FileManagement fileManagement)
    {
        return toAjax(fileManagementService.insertFileManagement(fileManagement));
    }

    /**
     * 修改文件管理
     */
    @GetMapping("/edit/{fileId}")
    public String edit(@PathVariable("fileId") Integer fileId, ModelMap mmap)
    {
        FileManagement fileManagement = fileManagementService.selectFileManagementById(fileId);
        mmap.put("fileManagement", fileManagement);
        return prefix + "/edit";
    }

    /**
     * 修改保存文件管理
     */
    @RequiresPermissions("course:file:edit")
    @Log(title = "文件管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FileManagement fileManagement)
    {
        return toAjax(fileManagementService.updateFileManagement(fileManagement));
    }

    /**
     * 删除文件管理
     */
    @RequiresPermissions("course:file:remove")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fileManagementService.deleteFileManagementByIds(ids));
    }
}
